import { Switch } from '@/api/storeMember';
import { FormSchema } from '@/components/Form';
import { usePermission } from '@/hooks/web/usePermission';
import { useDictStore } from '@/store/modules/dict';
import { renderImage } from '@/utils';
import { defRangeShortcuts } from '@/utils/dateUtil';
import { cloneDeep } from 'lodash-es';
import { NSwitch } from 'naive-ui';
import { h, ref } from 'vue';

const dict = useDictStore();
const { hasPermission } = usePermission();
const $message = window['$message'];

export class State {
  public id = 0; // id
  public storeId = 0; // 门店 id
  public memberId = 0; // 账号 id
  public isAdmin = 2; // 是否是管理员
  public createdAt = ''; // 创建时间
  public storePublicityImgs = []; // 门店图片
  public adminMemberAvatar = '';  // 头像

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  storeId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入门店 id',
  },
  memberId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入账号 id',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  // {
  //   field: 'id',
  //   component: 'NInputNumber',
  //   label: 'id',
  //   componentProps: {
  //     placeholder: '请输入id',
  //     onUpdateValue: (e: any) => {
  //       console.log(e);
  //     },
  //   },
  // },
  {
    field: 'adminMemberRealName',
    component: 'NInput',
    label: '用户名称',
    componentProps: {
      placeholder: '请输入用户名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'storeName',
    component: 'NInput',
    label: '门店名称',
    componentProps: {
      placeholder: '请输入门店名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'adminMemberMobile',
    component: 'NInput',
    label: '手机号码',
    componentProps: {
      placeholder: '请输入手机号码',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'isAdmin',
    component: 'NRadioGroup',
    label: '管理员',
    defaultValue: 0,
    giProps: {
      //span: 24,
    },
    componentProps: {
      options: dict.getOption('xxb_md_isAdmin'),
      onUpdateChecked: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '创建时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  // {
  //   field: 'adminMemberUsername',
  //   component: 'NInput',
  //   label: '帐号',
  //   componentProps: {
  //     placeholder: '请输入帐号',
  //     onUpdateValue: (e: any) => {
  //       console.log(e);
  //     },
  //   },
  // },
  {
    field: 'storePhone',
    component: 'NInput',
    label: '门店电话',
    componentProps: {
      placeholder: '请输入门店电话',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'ID',
    key: 'id',
    align: 'left',
    width: 60,
  },
  {
    title: '头像',
    key: 'adminMemberAvatar',
    align: 'center',
    width: 80,
    render(row: State) {
      return renderImage(row.adminMemberAvatar? row.adminMemberAvatar : "")
    },
  },
  {
    title: '用户名称',
    key: 'adminMemberRealName',
    align: 'left',
    width: 200,
  },
  {
    title: '手机号码',
    key: 'adminMemberMobile',
    align: 'center',
    width: 120,
  },
  {
    title: '门店图片',
    key: 'storePublicityImgs',
    align: 'center',
    width: 80,
    render(row: State) {
      return renderImage(row.storePublicityImgs? row.storePublicityImgs[0] : "")
    },
  },
  {
    title: '门店名称',
    key: 'storeName',
    align: 'left',
    width: -1,
  },
  {
    title: '门店电话',
    key: 'storePhone',
    align: 'center',
    width: 120,
  },
  {
    title: '管理员',
    key: 'isAdmin',
    align: 'center',
    width: 100,
    render(row: State) {
      return h(NSwitch, {
        value: row.isAdmin === 1,
        checked: '开启',
        unchecked: '关闭',
        disabled: true, //!hasPermission(['/storeMember/switch']),
        onUpdateValue: function (e) {
          console.log('onUpdateValue e:' + JSON.stringify(e));
          row.isAdmin = e ? 1 : 2;
          Switch({ id: row.id, key: 'is_admin', value: row.isAdmin }).then((_res) => {
            $message.success('操作成功');
          });
        },
      });
    },
  },
  {
    title: '创建时间',
    key: 'createdAt',
    align: 'center',
    width: 180,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['xxb_md_isAdmin']);
}